/*
 * Created on Aug 21, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ba360.struts.action;
 
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ba360.portal.categories.MainCat;
import com.ba360.portal.categories.SubCatList;
import com.ba360.portal.categories.TestList;
import com.ba360.portal.database.DBConnectionImpl;
import com.ba360.portal.util.Log;

/**
 * @author ROnat
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
/**
 * @author ROnat
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PortalStartUp extends HttpServlet {
	//Log log = null;
	
	 public void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException   {
	 	ServletContext application = getServletContext();
	 	Log.init(this.getClass());
		
	 	Log.info("FRom PortalStartup: "+application.getServerInfo());
	 	
	 }
	 
	 public void init(ServletConfig config) throws ServletException
	 {
	    
	 	super.init(config);
	 	ServletContext application = getServletContext();
	 	Log.init(this.getClass());
		
	 	Log.info("From PortalStartup init(config): "+application.getServerInfo());
	 	try{
		DBConnectionImpl dbConn = new DBConnectionImpl();
		Log.info("From PortalStartup init(config): instantiating dbConn");
		Connection conn = dbConn.getConnection();
		Log.info("From PortalStartup got connection to db:"+conn);
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt
				.executeQuery("select category_id,name from categories where par_category_id is null");
		MainCat[] mainCat = new MainCat[11];
		Log.init(this.getClass());
		Log.info("Fetch Size:" + rs.getFetchSize());
		int count = 0;
		List catList = new ArrayList();

		while (rs.next()) {
			Statement stmt1 = conn.createStatement();
			String mainCatName = rs.getString("name");
			int mainCatId = rs.getInt("category_id");

			ResultSet rs1 = stmt1
					.executeQuery("select category_id,name from categories where par_category_id="
							+ rs.getInt("category_id"));
			ArrayList subCat = new ArrayList();
			while (rs1.next()) {
				String subCatName = rs1.getString("name");
				int subCatId = rs1.getInt("category_id");

				SubCatList subCatList = new SubCatList(subCatName, subCatId);

				subCat.add(subCatList);
			}
			rs1.close();
			MainCat mainCatTemp = new MainCat(mainCatName, subCat);
			mainCatTemp.setMainCatId(mainCatId);
			mainCat[count] = mainCatTemp;
			count = count + 1;

			catList.add(mainCat);
		}
		rs.close();
		rs = stmt.executeQuery("select name,category_id from categories");
		Hashtable categories = new Hashtable();
		Hashtable categoryIds = new Hashtable();
		while (rs.next()) {
			int mainCatId = rs.getInt("category_id");
			String catName = rs.getString("name");

			categories.put(mainCatId + "", catName);
		}
		rs.close();

		rs = stmt
				.executeQuery("select category_id,par_category_id from categories");
		while (rs.next()) {
			int catId = rs.getInt("category_id");
			int parentCatId = rs.getInt("par_category_id");
			Log.info(catId + "  " + parentCatId);
			categoryIds.put(catId + "", parentCatId + "");
		}
		rs.close();

		application.setAttribute("APALLCATEGORY", categories);
		application.setAttribute("APCATIDS", categoryIds);
		TestList tl = new TestList(mainCat);
		application.setAttribute("APCATLIST", tl);//catList);

		stmt.close();
		conn.close();
	 	}catch (Exception e)
		{
	 		throw new ServletException(e.getMessage());
		}
	 }
	 
	 public void init() throws ServletException
	 {
	    
	 	super.init();
	 	ServletContext application = getServletContext();
	 	Log.init(this.getClass());		
	 	Log.info("FRom PortalStartup init(): "+application.getServerInfo());
	 	
	 }

}
